#include "input.h"

Input* Input::this_;

//Construction
Input::Input(int width, int height) : width_(width), height_(height), inputQueue_() {
    for(int i = 0; i < 256; i++) {
        keyDown_[i] = 0;
    }
	
	leftDown_ = false;
	rightDown_ = false;

    this_ = this;

	changed = false;
}

//Instance Retreival
Input* Input::getThis() {
    return this_;
}

//Callbacks
void Input::keyboard(unsigned char key, int x, int y) {
	//changed = true;
    //keyDown_[key] = 1;

	inputQueue_.push(ButtonPress(key, true));
}

void Input::keyboardUp(unsigned char key, int x, int y) {
    //changed = true;
	//keyDown_[key] = 0;

	inputQueue_.push(ButtonPress(key, false));
}

void Input::mouse(int x, int y) {
	changed = true;
    mouseLoc_.x = x - (width_ / 2);
    mouseLoc_.y = (height_ / 2) - y;
}

void Input::mouseClick(int button, int state, int x, int y) {
	changed = true;
	switch(button) {
	case GLUT_LEFT_BUTTON:
		if(state == GLUT_DOWN) {
			leftDown_ = true;
		} else {
			leftDown_ = false;
		}
		break;

	case GLUT_RIGHT_BUTTON:
		if(state == GLUT_DOWN) {
			rightDown_ = true;
		} else {
			rightDown_ = false;
		}
		break;
	}

	mouseLoc_.x = x - (width_ / 2);
    mouseLoc_.y = (height_ / 2) - y;
}

void Input::init() {
    //glutKeyboardFunc(keyboard);
    //glutKeyboardUpFunc(keyboardUp);
    //glutPassiveMotionFunc(mouse);
    //glutMouseFunc(buttons);
}

//Input retreival
bool Input::keyDown(int index) {
    if((index < 256) && (index >= 0)) {
        return keyDown_[index] == 1;
    }
    return false;
}

bool Input::operator[](char key) {
    int index = (int) key;
    if((index < 256) && (index >= 0)) {
        return keyDown_[index] == 1;
    }
    return false;
}

cpVect Input::getDirection() {
    cpVect direction;

    if(keyDown('a') && keyDown('d')) {
        direction.x = 0;
    } else if(keyDown('a')) {
        direction.x = -1.0;
    } else if(keyDown('d')) {
        direction.x = 1.0;
    } else {
        direction.x = 0;
    }

    if(keyDown('w') && keyDown('s')) {
        direction.y = 0;
    } else if(keyDown('w')) {
        direction.y = 1.0;
    } else if(keyDown('s')) {
        direction.y = -1.0;
    } else {
        direction.y = 0;
    }
    return direction;
}

void Input::clearChange() {
	changed = false;
}

